// merchant//merchants/selectWeek/index.js
const utils = require('../../utils/util.js');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    childUid:'',
    parentuid: '',
    isAll: 0,
    employeeid: 0,
    employeename: '',
    payment: 1,//1线上支付2线下支付
    themonth: '',//当前月
    curWeekarry: [],//当前月周数组
    perWeekarry: [],//上月周数组
    childType: 1//2所有分店汇总
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.curWeeks();
    if(options.childUid){
      this.setData({
        childUid: options.childUid,
        parentuid: options.parentuid,
        isAll: options.isAll,
        payment: options.payment
      })
    }
    if (options.employeeid){
      this.setData({
        employeeid: options.employeeid,
        employeename: options.employeename
      })
    }
  },
  selectWeek: function(e){
    let selectWeek = e.currentTarget.dataset.week;
    let weekno = e.currentTarget.dataset.weekno;
    let month = e.currentTarget.dataset.month;
    wx.redirectTo({
      url: '/merchant/merchants/businReports/index?selectWeek=' + selectWeek + '&weekno=' + weekno + '&month=' + month + '&childUid=' + this.data.childUid + '&parentuid=' + this.data.parentuid + '&isAll=' + this.data.isAll + '&payment=' + this.data.payment + '&employeeid=' + this.data.employeeid + '&employeename=' + this.data.employeename + '&childType=' + this.data.childType,
    })
  },
  curWeeks: function () {
    var curWeekarry = [];
    var today = new Date();
    var year = today.getFullYear();
    var month = today.getMonth();
    var start = new Date(year, month, 1); //当前月第一天的日期
    var end = new Date(year, month + 1, 0); //当前月最后一天的日期
    var start_day = start.getDay();
    let i = 0;
    switch (start_day) {
      case 0:
        i = 0 + 2;
        break;
      case 1:
        i = 1;
        break;
      case 2:
        i = 0 + 7;
        break;
      case 3:
        i = 0 + 6;
        break;
      case 4:
        i = 0 + 5;
        break;
      case 5:
        i = 0 + 4;
        break;
      case 6:
        i = 0 + 3;
        break;
    }
    while (new Date(year, month, i) <= end) {
      curWeekarry.push([new Date(year, month, i), new Date(year, month, i + 6)]);
      i += 7;
    }
    for (let j = 0; j < curWeekarry.length; j++){
      curWeekarry[j].push(utils.formatTime(curWeekarry[j][0]).replace(/\//g, ".").substr(5, 5) + '-' + utils.formatTime(curWeekarry[j][1]).replace(/\//g, ".").substr(5, 5));
    }
    this.perWeeks();
    this.setData({
      themonth: month+1,
      curWeekarry: curWeekarry
    })
  },

  perWeeks: function () {
    var perWeekarry = [];
    var today = new Date();
    var year = today.getFullYear();
    var month = today.getMonth();
    var start = new Date(year, month-1, 1); //上个月第一天的日期
    var end = new Date(year, month, 0); //上个月最后一天的日期
    var start_day = start.getDay();
    let i = 0;
    switch (start_day) {
      case 0:
        i = 0 + 2;
        break;
      case 1:
        i = 1;
        break;
      case 2:
        i = 0 + 7;
        break;
      case 3:
        i = 0 + 6;
        break;
      case 4:
        i = 0 + 5;
        break;
      case 5:
        i = 0 + 4;
        break;
      case 6:
        i = 0 + 3;
        break;
    }
    while (new Date(year, month-1, i) <= end) {
      perWeekarry.push([new Date(year, month-1, i), new Date(year, month-1, i + 6)]);
      i += 7;
    }
    for (let j = 0; j < perWeekarry.length; j++) {
      perWeekarry[j].push(utils.formatTime(perWeekarry[j][0]).replace(/\//g, ".").substr(5, 5) + '-' + utils.formatTime(perWeekarry[j][1]).replace(/\//g, ".").substr(5, 5));
    }
    this.setData({
      perWeekarry: perWeekarry
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }

})